﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using RTW.Core.Controllers;
using RTW.Servers;
using RTW.Web.Models;

namespace RTW.Web.Controllers
{
    public class HomeController : BaseController
    {

        public DeptServers deptservers;
        public HomeMenuServer homeMenuServe;
        public TMsgServer msgServer;
        public List<FileInfo> list1 ;
        public List<FileInfo> list2 ;
        public List<FileInfo> list3;
        public List<FileInfo> list4;
        public IHostingEnvironment hostingEnvironment;
        public HomeController(IHostingEnvironment _hostingEnvironment)
        {
            deptservers = new DeptServers();
            homeMenuServe = new HomeMenuServer();
            msgServer = new TMsgServer();
            list1 = new List<FileInfo>();
            list2 = new List<FileInfo>();
            list3 = new List<FileInfo>();
            list4 = new List<FileInfo>();
            hostingEnvironment = _hostingEnvironment;
        }
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        public async Task<IActionResult> HomePage()
        {
            BindInfo();
            var model = await homeMenuServe.GetOrderHomeMenuList();
            ViewBag.TMsg = await msgServer.GetAllMsgTo();
            return View(model);
        } 

        /// <summary>
        /// 单个已读
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> SetHome(string ID)
        {
            bool a= await msgServer.EditAllMsg(Convert.ToInt32(ID));
            return Json(a);
        }
        /// <summary>
        /// 全部已读
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> SetHomeMan()
        {
            bool a = await msgServer.EditAllMsgMan();
            return Json(MsgResult(a));
        }

        public void BindInfo()
        {
            string fileDir = hostingEnvironment.WebRootPath + @"\layuiadmin\download\";

            DirectoryInfo dir = new DirectoryInfo(fileDir + @"plug-in\");
            FileInfo[] dirinfo = dir.GetFiles();
            foreach (var f in dirinfo)
            {
                list1.Add(f);
            }

            dir = new DirectoryInfo(fileDir + @"learning\");
            dirinfo = dir.GetFiles();
            foreach (var f in dirinfo)
            {
                list2.Add(f);
            }

            dir = new DirectoryInfo(fileDir + @"pertinent\");
            dirinfo = dir.GetFiles();
            foreach (var f in dirinfo)
            {
                list3.Add(f);
            }

            dir = new DirectoryInfo(fileDir + @"else\");
            dirinfo = dir.GetFiles();
            foreach (var f in dirinfo)
            {
                list4.Add(f);
            }
            ViewBag.list1 = list1;
            ViewBag.list2 = list2;
            ViewBag.list3 = list3;
            ViewBag.list4 = list4;
        }

    }
}
